package com.youaishujuhui.devops.web.sys;

import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import com.youaishujuhui.devops.web.domain.manage.SysUser;
import com.youaishujuhui.devops.web.util.Common;
import com.youaishujuhui.devops.web.util.WebUtil;


/**
 * 
* @Description: 权限 标签实现 
* @author chendongming
* @date 2018年7月16日 下午5:33:21
*
 */
public class AuthTag extends TagSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private String resource;//资源，简单理解为url
	
	@Override
    public void setPageContext(PageContext pageContext) {
        this.pageContext=pageContext;
    }
	
    public int doStartTag() throws JspTagException{
    	if(auth()){//如果有权限标签体内容才写到jsp
    		return Tag.EVAL_BODY_INCLUDE;
    	}else{
    		return Tag.SKIP_BODY;
    	}
    }

	public int doEndTag() throws JspTagException{
        return Tag.EVAL_PAGE;
    }

	/**
	 * 校验当前登陆角色是否有权限(产品限制)
	 * @return
	 */
	private boolean auth() {
		String rs = getResource();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		
		SysUser sysUser = WebUtil.getLoginUser(request);
			
		//没有产品相关参数请求，说明请求的是系统管理，直接判断URI是否在权限范围
		if (sysUser.getResourceUris().contains(rs)) {
			return true;
		}

		return false;
		
//		String productId = request.getParameter(Common.PRODUCT_ID_PARAM_NAME);
//        //String productCode = request.getParameter(Common.PRODUCT_CODE_PARAM_NAME);
//		if(productId!=null){
//	         Map<String, Set<String>> productUriMap = sysUser.getProductIdUriMap();
//	         if (productUriMap != null && productUriMap.get(productId) != null && productUriMap.get(productId).contains(rs)) {
//	             return true;
//	         }
//	    }/*else if(productCode!=null){
//	        Map<String, Set<String>> productUriMap = loginUser.getProductCodeUriMap();
//	        if (productUriMap != null && productUriMap.get(productCode) != null && productUriMap.get(productCode).contains(rs)) {
//	            return true;
//	        }
//	    }
//	   	 return false;
	}

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = resource;
	}
	
}
